<?PHP
require_once "../../mainfile.php";
$sendEmail = true;

	require_once('include/Validate.php');
	$noYes = array('NO', 'YES');
	//Validate Email Addy
	if($noYes[Validate::email($_POST['email'], true)] == 'NO'){
		$_SESSION['subject'] = $_POST['subject'];
		$_SESSION['name'] = $_POST['name'];
		$_SESSION['email'] = $_POST['email'];
		$_SESSION['comments'] = $_POST['comments'];		
		$_SESSION['message'] = "Please enter a valid email address before submitting!";
	    redirect_header('index.php', 1, 'Please enter a valid email address before submitting!');
	}elseif($_POST['comments'] == ""){
		$_SESSION['subject'] = $_POST['subject'];
		$_SESSION['name'] = $_POST['name'];
		$_SESSION['email'] = $_POST['email'];
		$_SESSION['comments'] = $_POST['comments'];
		$_SESSION['message'] = "Please enter your comments before submitting!"; 
	    redirect_header('index.php', 1, 'Please enter your comments before submitting!');
	}else{
	
		if($xoopsModuleConfig['enableSpamFilter'] == 1){
			//check to see if we have spam
			if(strpos($_POST['comments'], '<a href')){
				$sendEmail = false;
			}

		}		
	
		$_SESSION['subject'] = $_POST['subject'];
		$_SESSION['name'] = $_POST['name'];
		$_SESSION['email'] = $_POST['email'];
		$_SESSION['comments'] = $_POST['comments'];
		$result = $xoopsDB->query( "SELECT recipient, subject FROM ".$xoopsDB->prefix("contact_form_recipients")." WHERE ID = '".$_POST['subject']."'" ) ;
		 if ( $result ) {
			 $myrow = $xoopsDB->fetchArray($result);
			 require_once XOOPS_ROOT_PATH."/class/xoopsmailer.php";
			 
			$xoopsMailer =& getMailer();
			$xoopsMailer->useMail(); 
			$xoopsMailer->setTemplateDir(XOOPS_ROOT_PATH.'/modules/contact_form/templates');
			$xoopsMailer->setTemplate("mailRecipient.tpl");
			$xoopsMailer->setToEmails($myrow['recipient']);
			$xoopsMailer->setFromEmail($_POST['email']);
			$xoopsMailer->setFromName($_SESSION['name']); 
			$xoopsMailer->setSubject($myrow['subject'] . " - FROM: ".$_SERVER['HTTP_HOST']);
			$xoopsMailer->assign("MESSAGE", $_POST['comments']); 
			if($sendEmail == true){
				$xoopsMailer->send();
			}			
			$_SESSION['success'] = 'true';
			header("Location:index.php");
			 
		}


	}
?>